// src/router/AuthRouter.tsx
import { Navigate, useLocation } from 'react-router-dom';
import type { JSX } from 'react';


// 路由守卫组件
const AuthRouter = ({ children }: { children: JSX.Element }) => {
  const location = useLocation();
  
  // 1. 检查是否存在 token
  if (!localStorage.getItem('token')) {
    // 重定向到登录页，并携带当前路径信息
    return (
      <Navigate 
        to="/login" 
        replace 
        state={{ from: location.pathname }} 
      />
    );
  }
  
  // 2. 存在 token 则渲染内容
  return children;
};

export default AuthRouter;